package com.equipment.management.controller;


import com.alibaba.fastjson.JSON;
import com.equipment.management.entity.QueryInfo;
import com.equipment.management.entity.SensorData;
import com.equipment.management.entity.User;
import com.equipment.management.dao.UserDao;
import com.equipment.management.service.UserService;
import com.equipment.management.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserDao userDao;



    @PostMapping("/login")
    public Result login(@RequestBody User user) throws Exception {
        log.info("登录参数：{}",user);
        User us = userService.getUserByMessage(user.getAccount(), user.getPassword());
        return us!=null?new Result(200,"登录成功"):new Result(500,"登录失败");

    }

    @GetMapping("/all")
    public Object findAllUser(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize,
            @RequestParam(name = "name", required = false) String name
            ) {
        return userService.findAllUser(pageNum,pageSize, name);
    }

    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) throws Exception {

        User us = userService.getUserByAccount(user.getAccount());
        if(us!=null){
            return  new Result(300,"用户已存在");
        }

        int i=userService.addUser(user);
        user.setCreate_time(new Date());
        return i > 0?new Result(200,"添加成功"):new Result(500,"添加失败");

    }


    @GetMapping("/deleteUser")
    public Result deleteUser(@RequestParam("id")int id){
        int i=userService.deleteUser(id);

        return i > 0?new Result(200,"删除成功"):new Result(500,"删除失败");

    }


    @PostMapping("/editUser")
    public Result editUser(@RequestBody User user){
        int i = userDao.editUser(user);
        return i > 0?new Result(200,"更新成功",user):new Result(500,"更新失败",user);
    }

    @RequestMapping("/baseuser")
    public String baseuser(@RequestBody User user){
        int i = userDao.insert(user);
        return i > 0 ?"success":"error";
    }



    @PostMapping("/getall")
    public String getUserList(QueryInfo queryInfo) {
        // 获取最大列表数和当前编号
        int number = userDao.getUserCounts("%" + queryInfo.getQuery() + "%");
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();

        List<SensorData> users = userDao.getAllUser("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("number", number);
        res.put("data", users);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    @RequestMapping("/userstate")
    public String updateUserState(@RequestParam("id")Integer id, @RequestParam("state")Boolean state){
        int i = userDao.updateState(id, state);
        return i > 0 ?"success":"error";
    }




    @RequestMapping("/getupdate")
    public String getUpdateUser(int id){
        User user = userDao.getUpdateUser(id);
        String s = JSON.toJSONString(user);
        return s;
    }


}
